<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API 状态测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .status {
            padding: 10px;
            margin: 10px 0;
            border-radius: 4px;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
        .checking {
            background-color: #fff3cd;
            color: #856404;
        }
        button {
            padding: 8px 16px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0069d9;
        }
        pre {
            background-color: #f8f9fa;
            padding: 10px;
            border-radius: 4px;
            overflow-x: auto;
        }
    </style>
</head>
<body>
    <h1>API 状态测试</h1>
    
    <div class="status checking" id="status">检查中...</div>
    
    <button id="check-button">重新检查</button>
    
    <h2>响应数据</h2>
    <pre id="response-data">-</pre>
    
    <h2>控制台日志</h2>
    <pre id="console-log"></pre>
    
    <script>
        // 重定向控制台输出到页面
        const consoleLog = document.getElementById('console-log');
        const originalConsoleLog = console.log;
        const originalConsoleError = console.error;
        
        console.log = function() {
            const args = Array.from(arguments);
            consoleLog.textContent += '> LOG: ' + args.map(arg => 
                typeof arg === 'object' ? JSON.stringify(arg, null, 2) : arg
            ).join(' ') + '\n';
            originalConsoleLog.apply(console, arguments);
        };
        
        console.error = function() {
            const args = Array.from(arguments);
            consoleLog.textContent += '> ERROR: ' + args.map(arg => 
                typeof arg === 'object' ? JSON.stringify(arg, null, 2) : arg
            ).join(' ') + '\n';
            originalConsoleError.apply(console, arguments);
        };
        
        // 状态元素
        const statusElement = document.getElementById('status');
        const responseDataElement = document.getElementById('response-data');
        const checkButton = document.getElementById('check-button');
        
        // 检查 API 状态
        async function checkApiStatus() {
            try {
                console.log('开始检查 API 状态...');
                statusElement.textContent = '检查中...';
                statusElement.className = 'status checking';
                
                console.log('发送请求到 /api/bailian/status');
                const response = await fetch('/api/bailian/status');
                
                console.log('收到响应:', {
                    status: response.status,
                    statusText: response.statusText,
                    headers: Object.fromEntries([...response.headers])
                });
                
                if (!response.ok) {
                    throw new Error(`API 状态检查失败，状态码: ${response.status}`);
                }
                
                const data = await response.json();
                console.log('响应数据:', data);
                
                responseDataElement.textContent = JSON.stringify(data, null, 2);
                statusElement.textContent = '正常';
                statusElement.className = 'status success';
            } catch (error) {
                console.error('状态检查错误:', error);
                responseDataElement.textContent = error.message;
                statusElement.textContent = '无法连接';
                statusElement.className = 'status error';
            }
        }
        
        // 页面加载时检查状态
        document.addEventListener('DOMContentLoaded', checkApiStatus);
        
        // 点击按钮时重新检查状态
        checkButton.addEventListener('click', checkApiStatus);
    </script>
</body>
</html>
